package com.dlyk.web;

import com.dlyk.model.Role;
import com.dlyk.result.R;
import com.dlyk.service.RoleService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 角色
 */
@RestController
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 查询所有角色（分页查询）
     */
    @PreAuthorize(value = "hasAuthority('role:list')")
    @GetMapping("/api/role/list")
    public R roleList(@RequestParam(required = false) Integer current) {

        // required = false  表示非必须，这个参数也可以不传，不传参数，默认查询第一页
        if (current == null) {
            current = 1;
        }

        PageInfo<Role> rolePageInfo = roleService.getRoleListByPage(current);

        return R.OK(rolePageInfo);
    }

    /**
     * 录入角色
     */
    @PreAuthorize(value = "hasAuthority('role:add')")
    @PostMapping("/api/role/add")
    public R addRole(Role role) {

        // 这里就不使用Query对象接收了，直接使用Role对象接收

        int add = roleService.addRole(role);

        return add >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑角色
     */
    @PreAuthorize(value = "hasAuthority('role:edit')")
    @PostMapping("/api/role/edit")
    public R editRole(Role role) {

        // 这里就不使用Query对象接收了，直接使用Role对象接收

        int edit = roleService.editRole(role);

        return edit >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 删除角色
     */
    @PreAuthorize(value = "hasAuthority('role:remove')")
    @DeleteMapping("/api/role/remove/{id}")
    public R removeRole(@PathVariable Integer id) {

        int remove = roleService.removeRole(id);

        return remove >= 1 ? R.OK() : R.FAIL();
    }

}

